Skip to content

Implement nexus-based activity cancels#2917

Merged
Sushisource merged 8 commits into
masterfrom
sj/activity-cancels-nexus
Jun 23, 2026
Merged

Implement nexus-based activity cancels#2917
Sushisource merged 8 commits into
masterfrom
sj/activity-cancels-nexus

Conversation

@Sushisource

Copy link
Copy Markdown
Member

What was changed

  • Allow activities to be cancelled via the built-in worker nexus channel without requiring heartbeating
  • Adds ActivityCancellationToken to the activity context help with this

Why?

Parity, improved experience, no need to heartbeat if there are no details to record.

Checklist

  1. Closes

  2. How was this tested:
    Added tests

  3. Any docs updates needed?

@Sushisource Sushisource force-pushed the sj/activity-cancels-nexus branch 3 times, most recently from c1d83e5 to a4c9a5a Compare June 15, 2026 23:37
@Sushisource Sushisource marked this pull request as ready for review June 15, 2026 23:50
@Sushisource Sushisource requested a review from a team as a code owner June 15, 2026 23:50
@Sushisource Sushisource force-pushed the sj/activity-cancels-nexus branch 3 times, most recently from 935e645 to 22ea206 Compare June 16, 2026 23:52
@maciejdudko maciejdudko force-pushed the sj/activity-cancels-nexus branch 2 times, most recently from cd6d9c1 to 487f99f Compare June 18, 2026 21:25
@Sushisource Sushisource force-pushed the sj/activity-cancels-nexus branch from 487f99f to 8b0202b Compare June 22, 2026 20:58
Comment thread temporal-sdk/src/main/java/io/temporal/activity/ActivityCancellationToken.java Outdated
Comment thread temporal-sdk/src/main/java/io/temporal/activity/ActivityExecutionContext.java Outdated
Comment thread temporal-sdk/src/main/java/io/temporal/worker/Worker.java Outdated
Comment thread temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java Outdated
@Sushisource Sushisource force-pushed the sj/activity-cancels-nexus branch from 8b0202b to c5ab593 Compare June 23, 2026 17:03

@maciejdudko maciejdudko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit, otherwise looks great! LGTM!

Comment on lines +27 to +36
CompletableFuture<Void> result = new CompletableFuture<>();
cancellationFuture.whenComplete(
(ignored, exception) -> {
if (exception == null) {
result.complete(null);
} else {
result.completeExceptionally(unwrapCompletionException(exception));
}
});
return result;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this can be simplified:

Suggested change
CompletableFuture<Void> result = new CompletableFuture<>();
cancellationFuture.whenComplete(
(ignored, exception) -> {
if (exception == null) {
result.complete(null);
} else {
result.completeExceptionally(unwrapCompletionException(exception));
}
});
return result;
return cancellationFuture.exceptionally(e -> throw unwrapCompletionException(e));

This will return a new dependent future on each call, so calling complete() on it won't mess up token's internal state.

@Sushisource Sushisource Jun 23, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't actually work since the unwrapped exception is checked unfortunately. End up needing to change the unwrapper to be just as long as this more or less.

Comment on lines +62 to +65
public ActivityCancellationToken getCancellationToken() {
throw new UnsupportedOperationException(
"getCancellationToken is not supported for local activities");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does make it harder to write an activity that works in both local and non-local context, though. But we can revisit it another time - throwing exception leaves the door open.

@Sushisource Sushisource merged commit e976113 into master Jun 23, 2026
18 checks passed
@Sushisource Sushisource deleted the sj/activity-cancels-nexus branch June 23, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants